package model;

import java.util.LinkedList;
import java.util.Observable;

import util.StateCode;
import view.GridPanel;

public class ChessArray extends Observable{
	public static final int ROWS=14;
	public static final int COLUMNS=15;
	private Chess[][] array=new Chess[ROWS][COLUMNS];
	private LinkedList<Chess> chesslist=new LinkedList<>();
	private boolean isMy=false;
	public ChessArray(){
		
	}
	public boolean SaveChess(Chess chess){
		int i=chess.getY(),j=chess.getX();
		if(array[i][j]==null)//如果棋盘数组【i】【j】为空
		{
			array[i][j]=chess;//将棋赋给棋盘数组【i】【j】
			chesslist.push(chess);//将chess元素推入此chesslist列表所表示的堆栈。
			setChanged();
			notifyObservers(StateCode.CHANGECHESSBOX);//通知状态改变棋盒
			isMy=isMy?false:true;
			if(hasFive(chess)){//如果棋子有5颗
				setChanged();//通知chess获取颜色
				notifyObservers(chess.getColor());
				isMy=false;
			}
			return true;
		}
		return false;
	}
	public boolean hasFive(Chess chess){
		int row=chess.getY();
		int col=chess.getX();
		int c=1;
		for(int j=col+1;j<COLUMNS&&array[row][j]!=null&&chess.equalColor(array[row][j]);j++) c++;
		for(int j=col-1;j>=0&&array[row][j]!=null&&chess.equalColor(array[row][j]);j--) c++;
		if(c>=5) return true;
		
		c=1;
		for(int j=row+1;j<COLUMNS&&array[j][col]!=null&&chess.equalColor(array[j][col]);j++) c++;
		for(int j=row-1;j>=0&&array[j][col]!=null&&chess.equalColor(array[j][col]);j--) c++;
		if(c>=5) return true;
		
		c=1;
		for(int i=row+1,j=col+1;j<COLUMNS&&array[i][j]!=null&&chess.equalColor(array[i][j]);i++,j++) c++;
		for(int i=row-1,j=col-1;j>=0&&array[i][j]!=null&&chess.equalColor(array[i][j]);i--,j--) c++;
		if(c>=5) return true;
		
		c=1;
		for(int i=row-1,j=col+1;j<COLUMNS&&array[i][j]!=null&&chess.equalColor(array[i][j]);i--,j++) c++;
		for(int i=row+1,j=col-1;j>=0&&array[i][j]!=null&&chess.equalColor(array[i][j]);i++,j--) c++;
		if(c>=5) return true;
		
		return false;
	}
	public void Clear(){
		for(int i=0;i<ROWS;i++)
			for(int j=0;j<COLUMNS;j++)
				array[i][j]=null;
	}
	public void regret(){
		Chess chess;
		for(int a=0;a<2;a++){
			chess=chesslist.pop();
			int i=chess.getY();
			int j=chess.getX();
			array[i][j]=null;
		}
	}
	public void replay(GridPanel gridpanel){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Clear();
				for(int i=chesslist.size()-1;i>=0;i--){
					Chess chess=chesslist.get(i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					setChess(chess);
					gridpanel.repaint();
				}
				setChanged();
				notifyObservers(StateCode.REPLAYOVER);
			}
		}).start();
	}
	public Chess getChess (int i,int j) {
		return array[i][j];
	}
	public void setChess(Chess chess) {
		int i=chess.getY(),j=chess.getX();
		array[i][j]=chess;
	}
	public LinkedList<Chess> getChesslist() {
		return chesslist;
	}
	public boolean isMy() {
		return isMy;
	}
	public void setMy(boolean isMy) {
		this.isMy = isMy;
	}
	

}
